package com.hck.app.service;

import com.hck.domain.consultation.model.entity.ConsultationReplyEntity;
import com.hck.domain.consultation.model.entity.HealthConsultationEntity;
import com.hck.domain.consultation.service.ConsultationDomainService;
import com.hck.domain.consultation.service.IConsultationService;
import com.hck.infrastructure.redis.IRedisService;
import com.hck.types.model.PageRequest;
import com.hck.types.model.PageResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ConsultationService class description here.
 *
 * @author 黄朝看
 * @date 2025/6/30
 */
@Service
public class ConsultationService implements IConsultationService {
    @Resource
    private ConsultationDomainService domainService;
    
    @Resource
    private IRedisService redisService;

    public Long createConsultation(HealthConsultationEntity consultation) {
        Long consultationId = domainService.createConsultation(consultation);
        
        // 创建咨询后，清除相关缓存
        if (consultation.getUserId() != null) {
            redisService.remove("consult:user:" + consultation.getUserId());
        }
        if (consultation.getDoctorId() != null) {
            redisService.remove("consult:doctor:" + consultation.getDoctorId());
        }
        
        return consultationId;
    }

    public void replyConsultation(Long consultationId, ConsultationReplyEntity reply) {
        domainService.replyConsultation(consultationId, reply);
        
        // 回复咨询后，清除相关缓存
        redisService.remove("consult:replies:" + consultationId);
        redisService.remove("consult:info:" + consultationId);
    }

    public HealthConsultationEntity getConsultationDetail(Long id) {
        // 1. 先从缓存获取咨询详情
        HealthConsultationEntity consultation = redisService.getValue("consult:info:" + id);
        
        if (consultation == null) {
            // 2. 缓存中没有，从数据库获取
            consultation = domainService.getConsultationDetail(id);
            
            if (consultation != null) {
                // 3. 将咨询详情缓存到Redis
                redisService.setValue("consult:info:" + consultation.getId(), consultation);
            }
        }
        
        return consultation;
    }

    public List<ConsultationReplyEntity> getReplies(Long consultationId) {
        // 1. 先从缓存获取回复列表
        List<ConsultationReplyEntity> replies = redisService.getValue("consult:replies:" + consultationId);
        
        if (replies == null) {
            // 2. 缓存中没有，从数据库获取
            replies = domainService.getReplies(consultationId);
            
            if (replies != null && !replies.isEmpty()) {
                // 3. 将回复列表缓存到Redis
                redisService.setValue("consult:replies:" + consultationId, replies);
            }
        }
        
        return replies;
    }

    public List<HealthConsultationEntity> getUserConsultations(Long userId) {
        // 1. 先从缓存获取用户咨询列表
        List<HealthConsultationEntity> consultations = redisService.getValue("consult:user:" + userId);
        
        if (consultations == null) {
            // 2. 缓存中没有，从数据库获取
            consultations = domainService.getUserConsultations(userId);
            
            if (consultations != null && !consultations.isEmpty()) {
                // 3. 将用户咨询列表缓存到Redis
                redisService.setValue("consult:user:" + userId, consultations);
            }
        }
        
        return consultations;
    }

    public List<HealthConsultationEntity> getDoctorConsultations(Long doctorId) {
        // 1. 先从缓存获取医生咨询列表
        List<HealthConsultationEntity> consultations = redisService.getValue("consult:doctor:" + doctorId);
        
        if (consultations == null) {
            // 2. 缓存中没有，从数据库获取
            consultations = domainService.getDoctorConsultations(doctorId);
            
            if (consultations != null && !consultations.isEmpty()) {
                // 3. 将医生咨询列表缓存到Redis
                redisService.setValue("consult:doctor:" + doctorId, consultations);
            }
        }
        
        return consultations;
    }

    public void closeConsultation(Long consultationId) {
        domainService.closeConsultation(consultationId);
        
        // 关闭咨询后，清除相关缓存
        redisService.remove("consult:info:" + consultationId);
    }
    
    @Override
    public PageResponse<HealthConsultationEntity> getUserConsultationsPage(Long userId, PageRequest pageRequest) {
        // 分页查询直接从数据库获取，不使用缓存（因为分页参数变化较多）
        return domainService.getUserConsultationsPage(userId, pageRequest);
    }
    
    @Override
    public PageResponse<HealthConsultationEntity> getDoctorConsultationsPage(Long doctorId, PageRequest pageRequest) {
        // 分页查询直接从数据库获取，不使用缓存（因为分页参数变化较多）
        return domainService.getDoctorConsultationsPage(doctorId, pageRequest);
    }
}  
